﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;

namespace MSBuild.Mercurial
{
    /// <summary>
    /// Base class for the tasks that can correct to a remote repository.
    /// </summary>
    public class AbstractRemoteTask : AbstractHgTask
    {
        /// <summary>
        /// The path to the repository.
        /// </summary>
        private string repository;

        /// <summary>
        /// Gets or sets the name of the user.
        /// </summary>
        public string User { get; set; }

        /// <summary>
        /// Gets or sets the password of the user.
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// Gets or sets the path to the repository.
        /// </summary>
        protected string Repository
        {
            get
            {
                // Check if the repository is an url and if a user is specified
                if (!string.IsNullOrEmpty(this.User)
                    && !string.IsNullOrEmpty(this.repository)
                    && this.repository.Contains("://"))
                {
                    Uri uri = new Uri(this.repository);
                    UriBuilder builder = new UriBuilder(uri);

                    // check if a user have been set in the url
                    if (string.IsNullOrEmpty(builder.UserName))
                    {
                        builder.UserName = Uri.EscapeDataString(this.User);
                        if (!string.IsNullOrEmpty(this.Password))
                        {
                            builder.Password = Uri.EscapeDataString(this.Password);
                        }

                        return builder.Uri.ToString();
                    }
                }

                return this.repository;
            }

            set
            {
                this.repository = value;
            }
        }
    }
}
